## Find Libraries

IF(NOT DISABLE_CRYPTO)
    # Find libcrypto
    FIND_PATH(LIBCRYPTO_INCLUDE NAMES openssl/md5.h openssl/sha.h)
    IF(LIBCHAOS_LINK_SHARED)
        FIND_LIBRARY(LIBCRYPTO_LIB  NAMES libcrypto.so)
    ELSEIF(LIBCHAOS_LINK_STATIC)
        FIND_LIBRARY(LIBCRYPTO_LIB  NAMES libcrypto.a)
    ELSE()
        FIND_LIBRARY(LIBCRYPTO_LIB  NAMES libcrypto.so libcrypto.a)
    ENDIF()

    IF(LIBCRYPTO_INCLUDE AND LIBCRYPTO_LIB)
        SET(LIBCHAOS_WITH_CRYPTO TRUE)
    ELSEIF(REQUIRE_CRYPTO)
        MESSAGE(SEND_ERROR "OpenSSL (libcrypto/libssl) support is required (REQUIRE_CRYPTO)")
    ENDIF()
ENDIF()

IF(NOT DISABLE_ZPNG)
    # Find zlib and libpng
    FIND_PATH(ZLIB_INCLUDE   NAMES zlib.h)
    IF(LIBCHAOS_LINK_SHARED)
        FIND_LIBRARY(ZLIB_LIB    NAMES libz.so)
    ELSEIF(LIBCHAOS_LINK_STATIC)
        FIND_LIBRARY(ZLIB_LIB    NAMES libzlibstatic.a libz.a)
    ELSE()
        FIND_LIBRARY(ZLIB_LIB    NAMES libz.so libzlibstatic.a libz.a)
    ENDIF()

    FIND_PATH(LIBPNG_INCLUDE NAMES png.h)
    IF(LIBCHAOS_LINK_SHARED)
        FIND_LIBRARY(LIBPNG_LIB  NAMES libpng.so)
    ELSEIF(LIBCHAOS_LINK_STATIC)
        FIND_LIBRARY(LIBPNG_LIB  NAMES libpng.a)
    ELSE()
        FIND_LIBRARY(LIBPNG_LIB  NAMES libpng.so libpng.a)
    ENDIF()

    IF(ZLIB_LIB AND ZLIB_INCLUDE AND LIBPNG_LIB AND LIBPNG_INCLUDE)
        SET(LIBCHAOS_WITH_PNG TRUE)
    ELSEIF(REQUIRE_ZPNG)
        MESSAGE(SEND_ERROR "PNG support is required (REQUIRE_ZPNG)")
    ENDIF()
ENDIF()

IF(NOT DISABLE_ZJPEG)
    # Find libjpeg
    FIND_PATH(LIBJPEG_INCLUDE NAMES jpeglib.h)
    IF(LIBCHAOS_LINK_SHARED)
        FIND_LIBRARY(LIBJPEG_LIB  NAMES libjpeg.so)
    ELSEIF(LIBCHAOS_LINK_STATIC)
        FIND_LIBRARY(LIBJPEG_LIB  NAMES libjpeg.a)
    ELSE()
        FIND_LIBRARY(LIBJPEG_LIB  NAMES libjpeg.so libjpeg.a)
    ENDIF()
    # Apparently libjpeg-turbo is not binary-compatible with libjpeg
    #FIND_LIBRARY(LIBJPEG_LIB  NAMES libturbojpeg.a libjpeg.a libturbojpeg.so libjpeg.so)

    IF(LIBJPEG_LIB AND LIBJPEG_INCLUDE)
        SET(LIBCHAOS_WITH_JPEG TRUE)
    ELSEIF(REQUIRE_ZJPEG)
        MESSAGE(SEND_ERROR "JPEG support is required (REQUIRE_ZJPEG)")
    ENDIF()
ENDIF()

IF(NOT DISABLE_ZWEBP)
    # Find libwebp
    FIND_PATH(LIBWEBP_INCLUDE NAMES webp/decode.h webp/encode.h)
    IF(LIBCHAOS_LINK_SHARED)
        FIND_LIBRARY(LIBWEBP_LIB  NAMES libwebp.so)
    ELSEIF(LIBCHAOS_LINK_STATIC)
        FIND_LIBRARY(LIBWEBP_LIB  NAMES libwebp.a)
    ELSE()
        FIND_LIBRARY(LIBWEBP_LIB  NAMES libwebp.so libwebp.a)
    ENDIF()

    IF(LIBWEBP_INCLUDE AND LIBWEBP_LIB)
        SET(LIBCHAOS_WITH_WEBP TRUE)
    ELSEIF(REQUIRE_ZWEBP)
        MESSAGE(SEND_ERROR "WebP support is required (REQUIRE_ZWEBP)")
    ENDIF()
ENDIF()

IF(NOT DISABLE_ZDATABASE)
    # Find sqlite3
    FIND_PATH(LIBSQLITE3_INCLUDE NAMES sqlite3.h)
    IF(LIBCHAOS_LINK_SHARED)
        FIND_LIBRARY(LIBSQLITE3_LIB  NAMES libsqlite3.so)
    ELSEIF(LIBCHAOS_LINK_STATIC)
        FIND_LIBRARY(LIBSQLITE3_LIB  NAMES libsqlite3.a)
    ELSE()
        FIND_LIBRARY(LIBSQLITE3_LIB  NAMES libsqlite3.so libsqlite3.a)
    ENDIF()

    IF(LIBSQLITE3_INCLUDE AND LIBSQLITE3_LIB)
        SET(LIBCHAOS_WITH_SQLITE3 TRUE)
    ELSEIF(REQUIRE_ZDATABASE)
        MESSAGE(SEND_ERROR "SQLite3 support is required (REQUIRE_ZDATABASE)")
    ENDIF()
ENDIF()
